﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for D_Referer
/// </summary>
namespace Bestlife.ERP.DataAccess
{
    public class D_Referer
    {
        public D_Referer()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        string apin = "";
        string iapin = "";
        string newiapin = "";
        Boolean returnvalue = false;
        public static String super(string Super_Distribuor)
        {
            string title = "";
            string name = "";
            string error = "";
            SqlConnection conn_new = new SqlConnection();
            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT [Giv_Name],[Title] FROM [Tbl_Distributor] where Dis_ID='" + Super_Distribuor + "'";
                //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);

                SqlDataReader rdR;
                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    name = rdR["Giv_Name"].ToString();
                    title = rdR["Title"].ToString();

                }
                else
                {
                    error = "Sorry, this does not seem to be a valid Referrer ID.";
                }
                rdR.Close();
                clsCon.conn_new.Close();
            }
            catch (Exception)
            {
                // Console.WriteLine(exx);
            }
            return title+name+error;
            
        }

        public static String Agent(string super)
        {
            string title = "";
            string name = "";
            string error = "";
            SqlConnection conn_new = new SqlConnection();
            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT [Giv_Name],[Title] FROM [Tbl_Distributor] where Dis_ID='" + super + "'";
                //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);

                SqlDataReader rdR;
                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    name = rdR["Giv_Name"].ToString();
                    title = rdR["Title"].ToString();

                }
                else
                {
                    error = "Sorry, this does not seem to be a valid Referrer ID.";
                }
                rdR.Close();
                clsCon.conn_new.Close();
            }
            catch (Exception)
            {
                // Console.WriteLine(exx);
            }
            return title + name + error;

        }

        public Boolean parentcheck(string supid,string agentid)
        {
            Boolean returnvalue = false;
            
            SqlConnection conn_new = new SqlConnection();
            try
            {
                if (supid == agentid)
                {
                    returnvalue = true;
                }
                else
                {
                    String connStr = ConfigurationManager.ConnectionStrings["MainConncrown"].ConnectionString;
                    Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                    String Ssql = "SELECT [apin],[dealercode],[introducercode],[iapin] FROM [bestlife_php].[dbo].[dealerplacement] WHERE dealercode='" + agentid + "'";
                    //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);
                    SqlDataReader rdR;
                    rdR = clsCon.ReadTable(Ssql);
                    if (rdR.Read())
                    {
                        apin = rdR["apin"].ToString();
                        iapin = rdR["iapin"].ToString();

                        rdR.Close();
                        clsCon.conn_new.Close();
                        returnvalue=apincheck(supid, iapin);
                        
                    }
                }  
            }
            catch (Exception)
            {
                // Console.WriteLine(exx);
            }

            return returnvalue;
        }

        public Boolean apincheck(string sup, string newiapin)
        {
            try
            {
                String connStrr = ConfigurationManager.ConnectionStrings["MainConncrown"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStrr);
                String findipin = "SELECT [apin],[dealercode],[introducercode],[iapin] FROM [bestlife_php].[dbo].[dealerplacement] WHERE dealercode='" + newiapin + "'";
                SqlDataReader rdR;
                rdR = clsCon.ReadTable(findipin);
                {

                    if (rdR.Read())
                    {
                        string pin = rdR["iapin"].ToString();
                        if (sup == pin)
                        {
                            returnvalue = true;
                            rdR.Close();
                            clsCon.conn_new.Close();
                            return returnvalue;
                        }
                        else if (pin == "1")
                        {
                            returnvalue= false;
                            return returnvalue;
                        }
                        else 
                        {
                            rdR.Close();
                            clsCon.conn_new.Close();
                            apincheck(sup, pin);
                        }
                    }
                }

                
            }
            catch(Exception)
            {
            }
            return returnvalue;
            }
 

    }
}